package com.hi5.csocso.controllers;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.hi5.csocso.dao.UserDao;
import com.hi5.csocso.domain.User;

@RequestMapping(value = UserDebugController.REQUEST_MAPPING)
public class UserDebugController {
    public static final String REQUEST_MAPPING = "/debug/user.html";
    private static final Logger LOG = LoggerFactory.getLogger(UserDebugController.class);
    private static final String VIEW_NAME = "userDebug";

    private UserDao userDao;

    @RequestMapping(params = "create")
    public String handleCreate(User user) {
        LOG.info("Create user" + user.getName());
        userDao.saveUser(user);
        return "redirect:" + REQUEST_MAPPING; // Does not work
    }

    @RequestMapping
    public String handleList(Model model) {
        LOG.info("List users");
        model.addAttribute("users", userDao.getAllUser());
        model.addAttribute("user", new User()); // For registration
        return VIEW_NAME;
    }

    public void setUserDao(UserDao userDao) {
        this.userDao = userDao;
    }
}
